package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.NoticeMessageBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.NoticeMessageQTO;
import com.yoga.dao.dao.NoticeMessageDao;
import com.yoga.dao.domain.NoticeMessageDO;
import com.yoga.manager.admin.AdminNoticeMessageManager;
import com.yoga.manager.beanUtil.NoticeMessageBeanUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/10/19.
 */
@Component
public class AdminNoticeMessageManagerImpl implements AdminNoticeMessageManager {
    Logger logger = LoggerFactory.getLogger(AdminNoticeMessageManagerImpl.class);

    @Resource
    private NoticeMessageDao messageDao;

    @Override
    public NoticeMessageBO addData(NoticeMessageBO messageBO) throws AppException {
        if (messageBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(messageBO.getMsgTitle())) {
            throw new AppException(AppErrorCodeEnum.MSG_TITLE_NULL);
        }
        if (StringUtils.isEmpty(messageBO.getMsgContent())) {
            throw new AppException(AppErrorCodeEnum.MSG_CONTENT_NULL);
        }
        NoticeMessageDO messageDO = NoticeMessageBeanUtil.noticeMassageBOToDO(messageBO);
        int count = messageDao.insert(messageDO);
        if (count <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }

        return NoticeMessageBeanUtil.noticeMassageDOToBO(messageDao.findById(messageDO.getId()));
    }

    @Override
    public Boolean updateData(NoticeMessageBO messageBO) throws AppException {
        if (messageBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (messageBO.getNoticeMessageId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        int count = messageDao.updateById(NoticeMessageBeanUtil.noticeMassageBOToDO(messageBO));
        if (count <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public Boolean deleteData(Long messageId) throws AppException {
        if (messageId == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        int count = messageDao.deleteById(messageId);
        if (count <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return true;
    }

    @Override
    public NoticeMessageBO findById(Long messageId) throws AppException {
        if (messageId == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        return NoticeMessageBeanUtil.noticeMassageDOToBO(messageDao.findById(messageId));
    }

    @Override
    public PageBO<NoticeMessageBO> findForPage(NoticeMessageQTO messageQTO) throws AppException {
        if (messageQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PageBO<NoticeMessageBO> pageBO = new PageBO<>();
        pageBO.setQuery(messageQTO);
        Integer count = messageDao.findForPageCount(messageQTO);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<NoticeMessageBO> boList = NoticeMessageBeanUtil.doList2BOList(messageDao.findForPage(messageQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(boList);

        return pageBO;
    }
}
